package homework;

import java.util.Scanner;

public class UserPage {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        UserManager userManager = new UserManager();
        while (true){
            System.out.println("--------海康集团----------");
            System.out.println("        1、注册");
            System.out.println("        2、登录");
            System.out.println("        3、退出");
            System.out.println("请选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            if (choice==1){
                System.out.println("请输入注册的用户名：");
                String username = scanner.next();
                System.out.println("请输入密码：");
                String password = scanner.next();
                User user = new User(username,password);

                try {
                    userManager.checkUsernameExists(username);
                    userManager.add(user);
                    System.out.println("注册成功");
                } catch (UsernameAlreadyExistsException e) {
                    System.out.println(e.getMessage());
                }
            }else if (choice==2){
                System.out.println("请输入用户名：");
                String username = scanner.next();
                System.out.println("请输入密码：");
                String password = scanner.next();
                User user = new User(username,password);

                try {
                    userManager.login(user);
                }catch (LoginFailException e){
                    System.out.println(e.getMessage());
                }
            }else if (choice==3){
                break;
            }else {
                System.out.println("选择无效，重新选择");
            }
        }
        scanner.close();
    }
}
